var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin({
    name: 'common',
    filename: "./build/js/common.js",
    minChunks: 2,
});
var DefinePlugin = new webpack.DefinePlugin({
    "process.env": {
        NODE_ENV: JSON.stringify("development")
    }
});
// var ResolverPlugin = new webpack.ResolverPlugin([
//     new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
// ]);
// var ProvidePlugin = new webpack.ProvidePlugin({
//     $: 'jquery',
//     React: 'react',
//     ReactDOM: 'react-dom',
//     //fetch:'imports?this=>global!exports?global.fetch!whatwg-fetch',
// });

var publicPath = 'http://localhost:3000/';
var hotMiddlewareScript = 'webpack-hot-middleware/client?reload=true&&timeout=2000';


// var UglifyJsPlugin = new webpack.optimize.UglifyJsPlugin({
//     compress: {
//         warnings: false
//     }
// });

module.exports = {
    context: __dirname + '/src/share',
    entry: {
        router: ['./index.jsx', hotMiddlewareScript],
    },
    devtool: 'inline-source-map',
    output: {
        path: path.resolve('./public'),
        filename: './build/js/[name].entry.js',
        publicPath: publicPath,
        chunkFilename: './js/chunk/[name].[chunkhash:5].chunk.js',
    },
    resolve: {
        // root: [
        //     path.join(__dirname, 'bower_components')
        // ],
        extensions: ['.js', '.jsx', '.css', '.scss'],
    },
    module: {
        loaders: [{
                test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
                loader: "file?name=fonts/[hash:8].[name].[ext]"
            }, {
                test: /\.(woff|woff2)$/,
                loader: "url?prefix=font/&limit=5000&name=fonts/[hash:8].[name].[ext]"
            }, {
                test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
                loader: "url?limit=10000&mimetype=application/octet-stream&name=fonts/[hash:8].[name].[ext]"
            }, {
                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
                loader: "url?limit=10000&mimetype=image/svg+xml&name=svg/[hash:8].[name].[ext]"
            }, {
                test: /\.(jpe?g|png|gif)$/i,
                loaders: [
                    'url?limit=10000&name=images/[hash:8].[name].[ext]',
                    'image-webpack?{optimizationLevel: 7, interlaced: false, pngquant:{quality: "65-90", speed: 4}, mozjpeg: {quality: 65}}'
                ]
            }, {
                test: /\.css$/,
                //loader: "style-loader!css-loader!postcss-loader"
                loader: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ["css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]", "postcss-loader"]
                })
            },
            // {
            //     test: /\.scss$/,
            //     loader: ExtractTextPlugin.extract("style-loader", "css-loader!sass-loader!postcss-loader")
            // },
            {
                test: /\.(js|jsx)$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader',
                query: {
                    presets: ['react', 'latest', 'stage-0']
                }
            }
        ]
    },
    // sassLoader: {
    //     includePaths: [
    //         path.resolve(__dirname, "./node_modules/compass-mixins/lib"),
    //         path.resolve(__dirname, "./node_modules/")
    //     ]
    // },
    plugins: [
        new webpack.LoaderOptionsPlugin({
            options: {
                postcss: function () {
                    return [require('autoprefixer'), require('precss')]
                }
            }
        }),
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin(),
        commonsPlugin,
        // UglifyJsPlugin,
        DefinePlugin,
        //ResolverPlugin,
        //ProvidePlugin,
        new ExtractTextPlugin({
            filename: './css/[name].min.css',
            allChunks: true
        }),
    ]
};